<template>
  <div style="width:100%;height:100%;" v-loading="loading">
    <el-dialog :title="`${ operateType == 'add' ? '添加' : '编辑' }用户`" :close-on-click-modal="false" :visible.sync="dialogVisible" width="30%" @close="closeHandler">
      <el-form :model="user" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="真实姓名" prop="chineseName">
          <el-input v-model="user.chineseName" auto-complete="new-password" ></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="userName">
          <el-input v-model="user.userName" auto-complete="new-password" ></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="user.mobile" auto-complete="new-password" ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="pwd" type="password" show-password auto-complete="new-password"  minlength="8"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="user.email" auto-complete="new-password"></el-input>
        </el-form-item>
        <el-form-item label="账号状态" prop="status" >
          <el-select v-model="user.status" placeholder="设置账号状态" >
            <el-option label="正常" value="1" key="1"></el-option>
            <el-option label="冻结" value="0" key="0"></el-option>
            <el-option label="删除" value="2" kef="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="closeHandler()">取消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">立即{{ `${ operateType == "add" ? "创建" : "修改" }` }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {adminSaveUrl} from '@/utils/common'
export default {
  props: {
    userDialog: {
      type: Boolean,
      default: false
    },
    popUser: {
      type: Object,
      default: null
    },
    operateType:{
      type:String,
      default:'add'
    }
  },
  watch: {
    popUser(n,o) {
      console.log("old,{}",o)
      this.user = n; 
    },
     userDialog(n,o) {
      console.log("old,{}",o)
      this.dialogVisible = n; 
    }
  },
  data() {
    var validatePass = (rule, value, callback) => {
    
      if(!!this.user.userName && !this.pwd){
        return callback();
      }
      if(!this.pwd){
          this.pwd = "";
      }
      let passwordPattern = /^[a-zA-Z0-9]{6,22}$/
      if (!passwordPattern.test(this.pwd)) {
        //   console.log(111)
        callback(new Error('密码6-22位数字/字母'));
      } else {
          console.log(222)
        callback();
      }
    };
    var validateAccount = (rule, value, callback) => {

      let accountPattern = /^[a-zA-Z0-9]{5,20}$/
      if (!accountPattern.test(value)) {
        callback(new Error('密码5-20位账号'));
      } else {
        callback();
      }
    };
    return {
      dialogVisible: false,
      loading: false,
      user: {
        adminId: 0,
        userName: "",
        mobile: "",
        chineseName: "",
        pwd: "",
        email: "",
        status: 1,
      },
      rules: {
        mobile: { required: true, message: "请输入员工电话", trigger: "blur" },
        chineseName: { required: true, message: "请输入姓名", trigger: "blur" },
        userName: [{ validator: validateAccount, trigger: "blur" }],
        pwd: [{ validator: validatePass, trigger: "blur" }]
      },
      parSysUser:{
          type:0
      },
      pwd:"",
    }
  },
  mounted() {

  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.saveUser();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    saveUser() {
      this.loading = true;
      var md5 = require("md5");
      if(this.pwd){
          this.user.pwd = md5(this.pwd);
      }
      
      this.axios
        .post(adminSaveUrl, this.user)
        .then(res => {
          if (res.data.status == "SUCCESS") {
            this.loading = false;
            this.$emit("close");
            this.$message.success(res.data.result);
          } else {
            this.loading = false;
            this.$message.error(res.data.result);
          }
        })
        .catch(response => {
          this.loading = false;
          console.log("response - " + response);
          this.$message.error("数据保存异常失败");
        });
    },
    closeHandler: function () {
      this.dialogVisible = false;
      this.user = {};
      this.$emit("close");
    }
  }
};
</script>

<style lang="less" scoped>

</style>
